/**
数据验证（表单验证）
来自 grace.hcoder.net 
作者 hcoder 深海
*/
module.exports = {
	error:'',
	check : function (data, rule){
		for(var i = 0; i < rule.length; i++){
			if (!rule[i].checkType){return true;}
			if (!rule[i].name) {return true;}
			if (!rule[i].errorMsg) {return true;}
			// if (!data[rule[i].name]) {this.error = rule[i].errorMsg; return false;}
			switch (rule[i].checkType){
				case 'string':
					var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
					if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
				break;
				case 'int':
					var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$');
					if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
					break;
				break;
				case 'between':
					if(data[rule[i].name]){
						if (!this.isNumber(data[rule[i].name])){
							this.error = rule[i].errorMsg;
							return false;
						}
						var minMax = rule[i].checkRule.split(',');
						minMax[0] = Number(minMax[0]);
						minMax[1] = Number(minMax[1]);
						if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
							this.error = rule[i].errorMsg;
							return false;
						}
					}
				break;
				case 'betweenD':
					if(data[rule[i].name]){
						var reg = /^-?[1-9][0-9]?$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
						var minMax = rule[i].checkRule.split(',');
						minMax[0] = Number(minMax[0]);
						minMax[1] = Number(minMax[1]);
						if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
							this.error = rule[i].errorMsg;
							return false;
						}
					}
				break;
				case 'betweenF': 
					var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
					if (!reg.test(data[rule[i].name])){this.error = rule[i].errorMsg; return false;}
					var minMax = rule[i].checkRule.split(',');
					minMax[0] = Number(minMax[0]);
					minMax[1] = Number(minMax[1]);
					if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
						this.error = rule[i].errorMsg;
						return false;
					}
				break;
				case 'same':
					if (data[rule[i].name] != rule[i].checkRule) { this.error = rule[i].errorMsg; return false;}
				break;
				case 'notsame':
					if (data[rule[i].name] == rule[i].checkRule) { this.error = rule[i].errorMsg; return false; }
				break;
				case 'email':
					if(data[rule[i].name]){
						var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'phoneno':
					if(data[rule[i].name]){
						var reg = /^1[0-9]{10,10}$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'zipcode':
					if(data[rule[i].name]){
						var reg = /^[0-9]{6}$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'reg':
					var reg = new RegExp(rule[i].checkRule);
					if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
				break;
				case 'in':
					if(rule[i].checkRule.indexOf(data[rule[i].name]) == -1){
						this.error = rule[i].errorMsg; return false;
					}
				break;
				case 'notnull':
					if(data[rule[i].name] == null || data[rule[i].name].length < 1){this.error = rule[i].errorMsg; return false;}
				break;
				case 'number':
					if(data[rule[i].name]){
						var reg = /^[0-9]*$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'idcard':
					if(data[rule[i].name]){
						var reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'mobile':
					if(data[rule[i].name]){
						var reg = /^((0\d{2,3})?(-)?\d{7,8})|(1\d{10})$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'max':
					if(data[rule[i].name]){
						if (!this.isNumber(data[rule[i].name])){
							this.error = rule[i].errorMsg;
							return false;
						}
						var max = Number(rule[i].checkRule);
						if (data[rule[i].name] > max) {
							this.error = rule[i].errorMsg;
							return false;
						}
					}
				break;
				case 'integer':
					if(data[rule[i].name]){
						var reg = /^[-0-9]{1}\d*$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'positiveInteger':
					if(data[rule[i].name]){
						var reg = /^([1-9]\d*)$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'positiveIntegerZero':
					if(data[rule[i].name]){
						var reg =  /^(0|[1-9]\d*)$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'pwd':
					if(data[rule[i].name]){
						var reg =  /^[0-9a-zA-Z!@#\$%\^&\*\\\[\]\{\}\-\?\/]{6,16}$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'loginId':
					if(data[rule[i].name]){
						var reg =  /^[0-9a-zA-Z]{6,16}$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'bankCardNumber':
					if(data[rule[i].name]){
						var reg =  /^\d{16,19}$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				case 'xiaoshu2':	// 小数点2位
					if(data[rule[i].name]){
						var reg = /^\d{1,8}(\.\d{0,2})?$/;
						if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
					}
				break;
				// /^\d{1,8}(\.\d{0,2})?$/.test(e.detail.value)
			}
		}
		return true;
	},
	isNumber : function (checkVal){
		var reg = /^0|(-?[1-9][0-9]?.?[0-9]*)$/;
		return reg.test(checkVal);
	}
}